var dialogId;
var targetId;			//父页面所选用户ID
var targetType;
var nodeIds;	//所选节点ID串
var nodeNames;			//父页面所选节点名称串
var ysf;	//运算符
var ljgx;	//逻辑关系
var table;	//数据表
var cols;	//数据表对应的列
var authTypeListStr;	//权限类型下拉框数据
var authServiceId;		//服务ID

var tableId;	//所选数据表ID
var authType;	//所选权限类型

var leftStr = "[{id: '(', text: '('},{id: '((', text: '(('},{id: '(((', text: '((('}]";
var rightStr = "[{id: ')', text: ')'},{id: '))', text: '))'},{id: ')))', text: ')))'}]";

$(function(){
	initTableCombox();
});

/**
 * 初始化数据表下拉框
 */
function initTableCombox(){
	$('#dataTable').combobox({
		onChange: function(newValue, oldValue){
			tableId = newValue;
			getCols(newValue);	//获取数据表下的字段
			//删除表格所有行
			$("#test").find("tr").remove();
			addtr('test');	//新增行
		}
	});
	
	$('#authType').combobox({
		onChange: function(newValue, oldValue){
			authType = newValue;
		}
	});
}

/**
 * 加载数据表下的字段
 * @param tableId 表ID
 */
function getCols(tableId){
	$.ajax({
		url : 'platform/UserAuthSetController/getTableCol',
		async : false,
		data : {
			nodeIds : nodeIds,
			tableId : tableId
		},
		type : 'post',
		dataType : 'json',
		success : function(result) {
			cols = result.cols;
		}
	});
}

/**
 * 添加行
 */
function addtr(id) {
	var tr_id = $("#test>tbody>tr:last").attr("id");
	if(null == tr_id){
		tr_id = 0;
	}
	else{
		var bool = validateQualification();	//验证上一行数据合法性
		if(!bool){
			return;
		}
	}
	tr_id++;
	var str = "";
	//第一行没有删除功能
	if(tr_id != 1){
		str =
			"<tr id=\""+tr_id+"\" style=\"border: 0px;\">\n" +
			"  <td style=\"border: 0px;width:70px;\">\n" + 
			"    <input id=\"zkh"+tr_id+"\" name=\"zkh"+tr_id+"\" class=\"easyui-combobox\" data-options=\"valueField: 'id',textField: 'text',data:"+leftStr+",editable:false,width:40,onShowPanel:comboboxOnShowPanel,panelHeight:'auto'\" >\n" + 
			"  </td>\n" + 
			"  <td style=\"border: 0px;width:145px;\">\n" + 
			"    <input id=\"zd"+tr_id+"\" name=\"zd"+tr_id+"\" class=\"easyui-combobox\" data-options=\"valueField: 'id',textField: 'text',data:"+cols+",editable:false,width:120\" >\n" + 
			"  </td>\n" + 
			"  <td style=\"border: 0px;width:145px;\">\n" + 
			"    <input id=\"ysf"+tr_id+"\" name=\"ysf"+tr_id+"\" class=\"easyui-combobox\" data-options=\"valueField: 'id',textField: 'text',data:"+ysf+",editable:false,width:120\">\n" + 
			"  </td>\n" + 
			"  <td style=\"border: 0px;width:145px;\">\n" + 
			"    <input id=\"val"+tr_id+"\" name=\"val"+tr_id+"\" type=\"text\" class=\"easyui-validatebox\" onfocus=\"onValInput('"+tr_id+"')\" onblur=\"onValInputAffter('"+tr_id+"')\" style=\"width:120px;\" />\n" + 
			"  </td>\n" + 
			"  <td style=\"border: 0px;width:70px;\">\n" + 
			"    <input id=\"ykh"+tr_id+"\" name=\"ykh"+tr_id+"\" class=\"easyui-combobox\" data-options=\"valueField: 'id',textField: 'text',data:"+rightStr+",editable:false,width:40,onShowPanel:comboboxOnShowPanel,panelHeight:'auto'\" >\n" + 
			"  </td>\n" + 
			"  <td style=\"border: 0px;width:145px;\">\n" + 
			"    <input id=\"ljgx"+tr_id+"\" name=\"ljgx"+tr_id+"\" class=\"easyui-combobox\" data-options=\"valueField: 'id',textField: 'text',data:"+ljgx+",editable:false,width:120\">\n" + 
			"  </td>\n" + 
			"  <td style=\"border: 0px; width:27px;\">\n" + 
			"    <a   onclick=\"addtr('test');\"\n" + 
			"        href=\"javascript:void(0);\" ><img  src=\"avicit/im/lantop/common/search/image/add_88008.png\" style=\"border: 0px; margin-top:7px;\"></a>\n" + 
			"  </td>\n" + 
			"  <td style=\"border: 0px; width:27px;\">\n" + 
			"    <a  onclick=\"deltr(this);\"\n" + 
			"        href=\"javascript:void(0);\" ><img  src=\"avicit/im/lantop/common/search/image/icon_sub_black.png\" style=\"border: 0px; margin-top:7px;\"></a>\n" + 
			"  </td>\n" + 
			"</tr>";
	}
	else{
		str =
			"<tr id=\""+tr_id+"\" style=\"border: 0px;\">\n" +
			"  <td style=\"border: 0px;width:70px;\">\n" + 
			"    <input id=\"zkh"+tr_id+"\" name=\"zkh"+tr_id+"\" class=\"easyui-combobox\" data-options=\"valueField: 'id',textField: 'text',data:"+leftStr+",editable:false,width:40,onShowPanel:comboboxOnShowPanel,panelHeight:'auto'\" >\n" + 
			"  </td>\n" + 
			"  <td style=\"border: 0px;width:145px;\">\n" + 
			"    <input id=\"zd"+tr_id+"\" name=\"zd"+tr_id+"\" class=\"easyui-combobox\" data-options=\"valueField: 'id',textField: 'text',data:"+cols+",editable:false,width:120\" >\n" + 
			"  </td>\n" + 
			"  <td style=\"border: 0px;width:145px;\">\n" + 
			"    <input id=\"ysf"+tr_id+"\" name=\"ysf"+tr_id+"\" class=\"easyui-combobox\" data-options=\"valueField: 'id',textField: 'text',data:"+ysf+",editable:false,width:120\">\n" + 
			"  </td>\n" + 
			"  <td style=\"border: 0px;width:145px;\">\n" + 
			"    <input id=\"val"+tr_id+"\" name=\"val"+tr_id+"\" type=\"text\" class=\"easyui-validatebox\" onfocus=\"onValInput('"+tr_id+"')\" onblur=\"onValInputAffter('"+tr_id+"')\" style=\"width:120px;\" />\n" + 
			"  </td>\n" + 
			"  <td style=\"border: 0px;width:70px;\">\n" + 
			"    <input id=\"ykh"+tr_id+"\" name=\"ykh"+tr_id+"\" class=\"easyui-combobox\" data-options=\"valueField: 'id',textField: 'text',data:"+rightStr+",editable:false,width:40,onShowPanel:comboboxOnShowPanel,panelHeight:'auto'\" >\n" + 
			"  </td>\n" + 
			"  <td style=\"border: 0px;width:145px;\">\n" + 
			"    <input id=\"ljgx"+tr_id+"\" name=\"ljgx"+tr_id+"\" class=\"easyui-combobox\" data-options=\"valueField: 'id',textField: 'text',data:"+ljgx+",editable:false,width:120\">\n" + 
			"  </td>\n" + 
			"  <td style=\"border: 0px; width:27px;\">\n" + 
			"    <a  onclick=\"addtr('test');\"\n" + 
			"        href=\"javascript:void(0);\" ><img  src=\"avicit/im/lantop/common/search/image/add_88008.png\" style=\"border: 0px; margin-top:7px;\"></a>\n" + 
			"  </td>\n" + 
			"  <td style=\"border: 0px;width:27px;\"> \n" + 
			"  </td>\n" + 
			"</tr>";
	}
	
	$('#' + id).append(str);
	$.parser.parse('#' + tr_id);
	
	$('#val'+tr_id).val("字段值输入");
	//字段选择
	$('#zd'+tr_id).combobox({
		onShowPanel : function(){
			$('#zd'+tr_id).combobox('setText','');
		},
		onHidePanel : function(){
			if($('#zd'+tr_id).combobox("getText") == ""){
				$('#zd'+tr_id).combobox('setText','字段选择');
			}
		}
	});
	$('#zd'+tr_id).combobox('setText','字段选择');
	//运算符
	$('#ysf'+tr_id).combobox({
		onShowPanel : function(){
			$('#ysf'+tr_id).combobox('setText','');
		},
		onHidePanel : function(){
			if($('#ysf'+tr_id).combobox("getText") == ""){
				$('#ysf'+tr_id).combobox('setText','运算符选择');
			}
		}
	});
	$('#ysf'+tr_id).combobox('setText','运算符选择');
	//逻辑关系
	$('#ljgx'+tr_id).combobox({
		onShowPanel : function(){
			$('#ljgx'+tr_id).combobox('setText','');
		},
		onHidePanel : function(){
			if($('#ljgx'+tr_id).combobox("getText") == ""){
				$('#ljgx'+tr_id).combobox('setText','或者/并且选择');
			}
		}
	});
	$('#ljgx'+tr_id).combobox('setText','或者/并且选择');
	
	$('#zkh' + tr_id).combobox('select', '(');
	$('#ykh' + tr_id).combobox('select', ')');
}

/**
 * 删除行
 */
function deltr(clickTd) {
	var maxId = $("#test>tbody>tr:last").attr("id");
	var delId = $(clickTd).parent().parent().attr("id");
	if(maxId == delId){
		//删除的行为最后一行
		$('#ljgx'+(delId-1)).combobox('setValue',"");	
	}
	var tr = $(clickTd).parent().parent();
	tr.remove();
}

/**
 * 保存新增条件
 */
function saveQualification(){
	if((null != $("#startDate").combo("getValue") || "" != $("#startDate").combo("getValue")) && (null != $("#endDate").combo("getValue") || "" != $("#endDate").combo("getValue"))){
		if($("#startDate").combo("getValue") > $("#endDate").combo("getValue")){
			//commonLang.tip.alert替换'提示',commonLang.optip.save.startDateNotLateEnd替换'开始日期不能大于结束日期！'
			$.messager.alert(commonLang.tip.alert, commonLang.optip.save.startDateNotLateEnd, 'warning');
			return;
		}
	}
	var bool = validateQualification(true);
	if(!bool){
		return bool;
	}
	var formJson = serializeObject($('#formQualification').form());
	var maxId = $("#test>tbody>tr:last").attr("id");
	var tableHtml = "";
	for(var i = 1; i <= maxId; i++){
		var zhkValue = $("#zkh"+i).combobox('getValue');
		//判断第i行左括号是否为空，以判断该行是否存在
		if(zhkValue != null && zhkValue != ""){
			tableHtml += 
				"<tr id=\""+i+"\" style=\"border: 0px;\">\n" +
				"  <td style=\"border: 0px;\">\n" + 
				"    <input id=\"zkh"+i+"\" name=\"zkh"+i+"\" class=\"easyui-combobox\" data-options=\"valueField: 'id',textField: 'text',data:"+leftStr+",editable:false,width:40,onShowPanel:comboboxOnShowPanel,panelHeight:'auto'\" value=\""+$('#zkh'+i).combobox('getValue')+"\" >\n" + 
				"  </td>\n" + 
				"  <td style=\"border: 0px;\">\n" + 
				"    <input id=\"zd"+i+"\" name=\"zd"+i+"\" class=\"easyui-combobox\" data-options=\"valueField: 'id',textField: 'text',width:120,data:"+cols+",editable:false\" value=\""+$('#zd'+i).combobox('getValue')+"\">" + 
				"  </td>\n" + 
				"  <td style=\"border: 0px;\">\n" + 
				"    <input id=\"ysf"+i+"\" name=\"ysf"+i+"\" class=\"easyui-combobox\" data-options=\"valueField: 'id',textField: 'text',width:120,data:"+ysf+",editable:false\" value=\""+$('#ysf'+i).combobox('getValue')+"\">" + 
				"  </td>\n" + 
				"  <td style=\"border: 0px;\">\n" + 
				"    <input id=\"val"+i+"\" name=\"val"+i+"\" type=\"text\" class=\"easyui-validatebox\" value=\""+$('#val'+i).val()+"\" style=\"width:120px;\"/>\n" + 
				"  </td>\n" + 
				"  <td style=\"border: 0px;\">\n" + 
				"    <input id=\"ykh"+i+"\" name=\"ykh"+i+"\" class=\"easyui-combobox\" data-options=\"valueField: 'id',textField: 'text',data:"+rightStr+",editable:false,width:40,onShowPanel:comboboxOnShowPanel,panelHeight:'auto'\" value=\""+$('#ykh'+i).combobox('getValue')+"\" >\n" + 
				"  </td>\n" + 
				"  <td style=\"border: 0px;\">\n" + 
				"    <input id=\"ljgx"+i+"\" name=\"ljgx"+i+"\" class=\"easyui-combobox\" data-options=\"valueField: 'id',textField: 'text',width:120,data:"+ljgx+",editable:false\" value=\""+$('#ljgx'+i).combobox('getValue')+"\">" + 
				"  </td>\n" + 
				"  <td style=\"border: 0px; width:50px;\">\n" + 
				"    <a class=\"easyui-linkbutton\" plain=\"false\" iconCls=\"\" onclick=\"addtr('test');\"\n" + 
				"        href=\"javascript:void(0);\" >+</a>\n" + 
				"  </td>\n";
			if(i != 1){
				//第一行没有删除
				tableHtml += 
					"  <td style=\"border: 0px; width:50px;\">\n" + 
					"    <a class=\"easyui-linkbutton\" plain=\"false\" iconCls=\"\" onclick=\"deltr(this);\"\n" + 
					"        href=\"javascript:void(0);\" >-</a>\n" + 
					"  </td>\n";
			}
			tableHtml += "</tr>";
		}
	}
	MaskUtil.mask();
	$.ajax({
		url : 'platform/UserAuthSetController/saveQualification',
		async : false,
		data : {
			targetType : targetType,
			targetId : targetId,
			authServiceId : authServiceId,
			nodeIds : nodeIds,
			nodeNames : nodeNames,
			tableId : tableId,
			authType : authType,
			formJson : JSON.stringify(formJson),
			maxId : maxId,
			tableHtml : tableHtml,
			ysf : ysf,
			ljgx : ljgx
		},
		type : 'post',
		dataType : 'json',
		success : function(result) {
			MaskUtil.unmask();
			parent.savequalificationAfter();
			closeWindow();
		}
	});
}

/**
 * 验证数据权限合法性
 */
function validateQualification(isSave){
	var tr_id = $("#test>tbody>tr:last").attr("id");
	//数据表对应的字段
	if($('#zd'+tr_id).combobox('getValue') == ""){
		//commonLang.tip.alert替换'提示',commonLang.optip.save.fieldNotNull替换'字段不能为空！'
		$.messager.alert(commonLang.tip.alert, commonLang.optip.save.fieldNotNull, 'warning');
		return false;
	}
	if($('#ysf'+tr_id).combobox('getValue') == ""){
		//commonLang.tip.alert替换'提示',commonLang.optip.save.operateNotNull替换'运算符不能为空！'
		$.messager.alert(commonLang.tip.alert, commonLang.optip.save.operateNotNull, 'warning');
		return false;
	}
	else{
		if($('#ysf'+tr_id).combobox('getValue') != "WK" && $('#ysf'+tr_id).combobox('getValue') != "BWK"){
			if($('#val'+tr_id).val() == "" || $('#val'+tr_id).val() == "字段值输入"){
				//commonLang.tip.alert替换'提示',commonLang.optip.save.valueNotNull替换'值不能为空！'
				$.messager.alert(commonLang.tip.alert, commonLang.optip.save.valueNotNull, 'warning');
				return false;
			}
		}
	}
	if(!isSave){
		if($('#ljgx'+tr_id).combobox('getValue') == ""){
			//commonLang.tip.alert替换'提示',commonLang.optip.save.logicRelateNotNull替换'逻辑关系不能为空！'
			$.messager.alert(commonLang.tip.alert, commonLang.optip.save.logicRelateNotNull, 'warning');
			return false;
		}
	}
	
	return true;
}

/**
 * 重置
 */
function reset(){
	$('#formQualification').form('reset');
	//删除表格所有行
	$("#test").find("tr").remove();
	addtr('test');	//新增行
}

/**
 * 关闭
 */
function closeWindow(){
	if (parent != null && parent.$('#' + dialogId) != null) {
		parent.$('#' + dialogId).dialog('close');
	}
}

/**
 * 开始字段输入：清空提示信息
 * @param rowId
 */
function onValInput(rowId){
	if($('#val'+rowId).val() == "字段值输入"){
		$('#val'+rowId).val("");
	}
}

/**
 * 字段输入结束：如果没有输入则显示提示信息
 * @param rowId
 */
function onValInputAffter(rowId){
	if($('#val'+rowId).val() == ""){
		$('#val'+rowId).val("字段值输入");
	}
}